// pages/apply/choosecourselist/choosecourselist.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require('../../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        typeall: 3,
        keyword: '',
        page: 0,
        listarr: [],
        errcode: -1,
        msg: '',
        isjfyc: false,
        bkitem: '',
        is_team: 1, //是否点击校区
        is_cj: 1, //是否点击入群状态
        // 已结清-未联系，待结清-待入群，已结清-未联系，待结清-已入群，已换课无需拉群，已退费无需拉群
        statusarr: [{ title: '全部', id: '' }, { title: '未联系', id: 1}, { title: '待入群', id: 2 },{ title: '已入群', id: 3 }],
        is_all_show: 1,
        cjname: '未联系',
        cjname1: '未联系',
        is_group:1, //成交id
        is_dl:0,
        log:'',
        isRecord:false,
        recordList:[],
        course_name:'',
        isCourse:false,
        toGroup:1
    },
    cloasebukuan(){
        this.setData({
            isRecord:false
        })
    },
    getCourse(){
        let isCourse=this.data.isCourse
        this.setData({
            isCourse:!isCourse
        })
    },
    getRecords(e){
        var that = this;
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        console.log(item);
        let obj={
            user_id:item.user_id,
            user_course_id:item.id,
            limit:30,
            page:1
        }
        share.wxRequest('crmapi/User/checkpullLog', obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                that.setData({
                    recordList: data.lists,
                    isRecord:true
                })
            }
            wx.hideLoading();
        })
    },
    searchclick(e){
        var that = this;
        var keyword = e.detail.value.keykown;
        that.setData({
            keyword: keyword,
            page:0
        })
        that.consCourses();
    },
    statusChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            cjname1: item.title,
            cjname: item.title,
            is_group: item.id
        })
    },
    statusclick() {
        var that = this;
        var is_cj = that.data.is_cj;
        if (Number(is_cj) == 1) {
            var is_all_show = 2;
        } else {
            var is_all_show = 1;
        }
        that.setData({
            is_all_show: is_all_show,
            is_cj: is_all_show, //是否点击成交人筛选
        })
    },
    closeclcikll() {
        var that = this;
        that.setData({
            is_cj: 1, //是否点击成交人筛选
            is_all_show: 1,
            page:0
        })
        that.consCourses();
    },
    // 点击重置
    closech() {
        var that = this;
        that.setData({
            name:'',
            cjname:'全部',
            cjname1:'全部',
            keyword: '',
            course_name:'',
            is_group:'',
            page:0
        })
        that.consCourses();
    },

    // 点击确定
    sureclick() {
        var that = this;
        // console.log(that.data.is_type_name1)
        that.setData({
            is_cj: 1, //是否点击成交人筛选
            is_all_show: 1,
            page:0
        })
        that.consCourses();
    },

    bukuanclick(e) {
        var that = this;
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            bkitem: item,
            isjfyc: true,
            toGroup:2,
            log:''
        })
       
    },
    bukuanclick1(e) {
        var that = this;
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            bkitem: item,
            isjfyc: true,
            toGroup:3,
            log:''
        })
       
    },

    // 取消订单
    noclick() {
        var that = this;
        that.setData({
            isjfyc: false
        })

    },
    copyphone(e){
        var that = this;
        var phone = e.currentTarget.dataset.phone || e.target.dataset.phone;
        wx.setClipboardData({
            data: phone,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: 'none',
                })
            },
            fail: function (res) {
                wx.showToast({
                    title: '复制失败',
                    icon: 'none',
                })
            },
        })
    },
    searchclfollwbutick(e){
        var that = this;
        var log = e.detail.value.log;
        console.log(e);
        if(log==''){
            wx.showToast({
                title: '请输入跟进记录',
                icon: 'none',
            })
        }else{
            that.setData({
                log: log,
            })
            that.yesclick();
        }
    },
    // 跟进记录
    yesclick() {
        var that = this;
        var item = that.data.bkitem;
        console.log(that.data.log);
        var consultant_id = app.globalData.admin_id;
        let obj={
            consultant_id:consultant_id,
            user_id:item.user_id,
            user_course_id:item.id,
            is_group:that.data.toGroup,//拉群状态 2-未入群已联系 3-已入群
            remarks:that.data.log,
            imgs:''
        }
        console.log(obj);
        share.wxRequest('crmapi/User/addpullLog', obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                wx.showToast({
                    title: '添加成功',
                    icon: 'none',
                })
            }
            that.setData({
                isjfyc:false,
                page:0,
                log:''
            })
            that.consCourses();
        })


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var type = options.type; //1退费 2换课 3全部课程
        that.setData({
            typeall: type,
            is_dl: Number(app.globalData.is_dl)
        })
        wx.setStorageSync('coursetype', type)
        if (Number(type) == 3) {
            wx.setNavigationBarTitle({
                title: '面授拉群',
            })
        }
    },

    consCourses() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var type = that.data.typeall;

        var consultant_id = app.globalData.admin_id;
        var page = that.data.page;
        var keyword = that.data.keyword;
        var course_name = that.data.course_name;
        var is_group = that.data.is_group;
        var isCourse = that.data.isCourse
        if (Number(type) == 3) {
            var pid  = '';
            var obj = {
                consultant_id: consultant_id,
                page: page,
                name: isCourse?'':keyword,
                course_name:isCourse?keyword:'',
                is_group:is_group,
            }
            var url = 'crmapi/User/pullgroupList'
    
        }
        share.wxRequest(url, obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var newslist = data;
                if (Number(page) == 0) {
                    var lists = newslist;
                } else {
                    var lists = that.data.listarr.concat(newslist);
                }

                if (Number(lists.length) == 0) {
                    var msg = '~暂无数据~'
                } else {
                    var msg = res.data.msg
                }
                that.setData({
                    errcode: res.data.errcode,
                    msg: msg,
                    listarr: lists,
                })
            } else {
                that.setData({
                    errcode: 1002,
                    msg: res.data.msg,
                    listarr: [],
                })
            }
            wx.hideLoading();
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        var type = wx.getStorageSync('coursetype');
        that.setData({
            typeall: type
        })
        if (wx.getStorageSync('keylistphone')) {
            that.setData({
                keyword: wx.getStorageSync('keylistphone')
            })
        } else {
            that.setData({
                keyword: ''
            })
        }
        that.consCourses();
    },



    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        var page = Number(that.data.page) + 1;
        that.setData({
            page: page,
        })
        that.consCourses();
    },
})